package com.opages.mvc.adapter.nacos.serviceregistry;

import com.opages.mvc.adapter.common.client.serviceregistry.Registration;
import com.opages.mvc.adapter.nacos.model.NacosDiscoveryProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.util.StringUtils;

import java.util.Map;

/**
 * Nacos注册<名词语义>信息
 * @author daizhong.liu
 *
 */
public class NacosRegistration implements Registration {
	private NacosDiscoveryProperties nacosDiscoveryProperties;

	public NacosRegistration(NacosDiscoveryProperties nacosDiscoveryProperties,String contextPath) {
		this.nacosDiscoveryProperties = nacosDiscoveryProperties;
		if(!StringUtils.isEmpty(contextPath)) {
			this.nacosDiscoveryProperties.getMetadata().put("contextPath",contextPath);
		}
	}

	@Override
	public String getServiceId() {
		return this.nacosDiscoveryProperties.getServiceName();
	}

	@Override
	public String getHost() {
		return this.nacosDiscoveryProperties.getHostname();
	}

	@Override
	public int getPort() {
		return this.nacosDiscoveryProperties.getPort();
	}

	@Override
	public Map<String, String> getMetadata() {
		return this.nacosDiscoveryProperties.getMetadata();
	}

	@Override
	public boolean isSecure() {
		return nacosDiscoveryProperties.isSecure();
	}

	@Override
	public String getScheme() {
		if(nacosDiscoveryProperties.isSecure()) {
			return "https";
		}
		return "http";
	}

	public NacosDiscoveryProperties getNacosDiscoveryProperties() {
		return nacosDiscoveryProperties;
	}
}
